//
//  MessageVC.m
//  TheProjectName
//
//  Created by 李劲成 on 16/5/19.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "MessageVC.h"
#import "UserListVC.h"
#import "ConversationListVC.h"

@interface MessageVC ()<UIScrollViewDelegate, EMContactManagerDelegate>


@property (nonatomic, retain) CGLCustomSegment *segmentOfTitle;

@property (nonatomic, retain) UIScrollView *scrollView;

@property (nonatomic, retain) UserListVC *listViewController;

@end

@implementation MessageVC



- (void)viewDidLoad {
    [super viewDidLoad];
    self.segmentOfTitle = [[CGLCustomSegment alloc] initWithItems:@[@"会话",@"联系人"]];
    [_segmentOfTitle addTarget:self action:@selector(titleSegmentAction:) forControlEvents:UIControlEventValueChanged];
    // 导航栏
    self.navigationItem.leftBarButtonItem = BARBUTTONIMAGE(@"aligh_left_27.291428571429px_1189116_easyicon.net", self, @selector(presentLeftMenuViewController:));
    self.navigationItem.titleView = _segmentOfTitle;
    // 添加联系人
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleAdd:)];
    [self createScrollView];
    [self createTwoSmallVC];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.layer.contents = (id)[MotiveManager shareMotiveManager].backImage.CGImage;

    
}

#pragma mark - 添加联系人
- (void)handleAdd:(UIBarButtonItem *)buttonItem {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"添加好友" message:nil preferredStyle: UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入用户名";
        
    }];
    [alert addAction:[UIAlertAction actionWithTitle:@"添加" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        EMError *error = [[EMClient sharedClient].contactManager addContact:alert.textFields.firstObject.text message:@"我想加您为好友"];
        if (!error) {
            [alert dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            UIAlertController *success = [UIAlertController alertControllerWithTitle:@"已发送添加申请" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:success animated:YES completion:^{
                [self performSelector:@selector(dismiss:) withObject:success afterDelay:1];
            }];
        }
        else {
            NSLog(@"1111-----------%@", error.description);
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:^{
            
        }];
    }]];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

#pragma mark - 子 VC
- (void)createTwoSmallVC {
    // 会话列表
    ConversationListVC *chatListVC = [[ConversationListVC alloc] init];
    chatListVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
    [self addChildViewController:chatListVC];
    [self.scrollView addSubview:chatListVC.view];
    
    // 联系人列表
    self.listViewController = [[UserListVC alloc] init];
    _listViewController.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
    [self addChildViewController:_listViewController];
    [self.scrollView addSubview:_listViewController.view];
}

#pragma makr - 创建scrollView
- (void)createScrollView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT - 64)];
    _scrollView.userInteractionEnabled = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}

#pragma mark - segment与scrollView关联
- (void)titleSegmentAction:(UISegmentedControl *)segment {
    
    CGFloat num = self.segmentOfTitle.selectedSegmentIndex;
    [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * num, 0) animated:YES];
}

#pragma mark - scrollView结束滚动，显示对应的segment
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat num = self.scrollView.contentOffset.x / SCREEN_WIDTH;
    self.segmentOfTitle.selectedSegmentIndex = num;
}

- (void)dismiss:(UIAlertController *)alert {
    [alert dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveAgreedFromUsername:(NSString *)aUsername{
    [_listViewController refresh];
}


@end
